package com.hongkzh.www.look.media.view.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hongkzh.www.R;
import com.hongkzh.www.look.media.model.bean.UserCategoryBean;
import com.hongkzh.www.look.model.bean.CategoryBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asad on 2018/3/23.
 * 自媒体 分类 adapter
 */

public class MedCarRvAdapter extends RecyclerView.Adapter<MedCarRvAdapter.ViewHolder> {
    private List<UserCategoryBean.DataBean> data = new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lkhrv, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.lkhrvTv.setTextColor(holder.itemView.getResources().getColor(R.color.color_FFF));
        if (data.size() == position || position == 9) {
            Glide.with(holder.itemView).load(R.mipmap.classify).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.lkhrvIma);
            holder.lkhrvTv.setText("全部分类");
        } else {
            Glide.with(holder.itemView).load(data.get(position).getImgSrc()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.lkhrvIma);
//            holder.lkhrvTv.setText(data.get(position).getCategory());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int type = 0;
//                if (position < 9) type = data.get(position).getType();
                if (onClassifyClickListener != null) onClassifyClickListener.onClassifyClick(type);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size() >= 10 ? 10 : data.size() + 1;
    }

    public void add(UserCategoryBean userCategoryBean) {
        if (userCategoryBean != null) data = userCategoryBean.getData();
    }

    public interface OnClassifyClickListener {
        void onClassifyClick(int type);
    }

    private OnClassifyClickListener onClassifyClickListener;

    public void setOnClassifyClickListener(OnClassifyClickListener onClassifyClickListener) {
        this.onClassifyClickListener = onClassifyClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lkhrv_ima)
        ImageView lkhrvIma;
        @BindView(R.id.lkhrv_tv)
        TextView lkhrvTv;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
